﻿using System;
using System.ComponentModel.Composition;
using System.Threading;

namespace Samples.MVVM.Models
{
    [Export(typeof(IWebService))]
    public class ChuckNorrisWebService : IWebService
    {
        private Random _random = new Random();
        private string[] _jokes = new string[]
        {
            "When Chuck Norris watches Dora, she doesn't ask any questions.",
            "When Alexander Bell invented the telephone he had 3 missed calls from Chuck Norris",
            "Chuck Norris thought Sonic was too slow.",
            "Chuck Norris doesn't call the wrong number. You answer the wrong phone.",
            "Chuck Norris's beard can type 140 wpm.",
            "Chuck Norris doesn'tt need to know about class factory pattern. He can instantiate interfaces.",
            "Chuck Norris doesn't need an OS.",
            "Chuck Norris can solve the Towers of Hanoi in one move",
            "Chuck Norris doesn't need the cloud to scale his applications, he uses his laptop.",
            "Chuck Norris doesn't have a CTRL key on his keyboard, no one controls Chuck Norris.",
            "Chuck Norris can access private members without using reflection.",
        };

        public event EventHandler DataRetrieved = delegate { };

        public void LoadData()
        {
            ThreadPool.QueueUserWorkItem(_ =>
            {
                //Just for demonstration
                Thread.Sleep(2000);

                //Grab a random joke
                Data = _jokes[_random.Next(0, _jokes.Length)];

                //Fire the event
                DataRetrieved(this, EventArgs.Empty);
            });
        }

        public string Data
        {
            get;
            private set;
        }

        public string Name
        {
            get { return "Chuck Norris Jokes"; }
        }
    }
}
